package com.wisdomparking.control.app;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wisdomparking.bean.Parking;
import com.wisdomparking.bean.User;
import com.wisdomparking.bean.simple.SimpleEcdemicParking;
import com.wisdomparking.bean.simple.SimpleParking;
import com.wisdomparking.config.mvc.BaseController;
import com.wisdomparking.config.mvc.Result;
import com.wisdomparking.config.mvc.ResultGenerator;
import com.wisdomparking.service.EcdemicParkingService;
import com.wisdomparking.service.ParkingService;
import com.wisdomparking.service.UserService;
import com.wisdomparking.tools.ToolValid;

import lombok.Value;

/**
 * 车场中心
 */
@RestController
@RequestMapping("/app/parkingCenter")
public class ParkingController extends BaseController{
	
	@Autowired ParkingService parkingService;
	@Autowired EcdemicParkingService ecdemicParkingService;
	@Autowired UserService userService;
	
	
	/**
	 * 获取车场停车位信息
	 * 
	 * @param name
	 * @return
	 */
	@RequestMapping("/getParkingSurplusPlace")
	public Result<Map<String, Object>> getParkingDetail(String name, HttpServletRequest request){
		Parking parking = parkingService.getParkingByName(name);
		Map<String, Object> map = null;
		if(ToolValid.isNotNull(parking)){
			map = new HashMap<>();
			map.put("parkingTotalA", parking.getParkingTotalA());
			map.put("surplusLargeVehicleA", parking.getSurplusLargeVehicleA());
			map.put("surplusLightdutyVehicleA", parking.getSurplusLightdutyVehicleA());
			map.put("chargingA", parking.getChargingA());
			map.put("parkingTotalB", parking.getParkingTotalB());
			map.put("surplusLargeVehicleB", parking.getSurplusLargeVehicleB());
			map.put("surplusLightdutyVehicleB", parking.getSurplusLightdutyVehicleB());
			map.put("chargingB", parking.getChargingB());
			
			int defaultVehicleType = 3;
			String userId = request.getParameter("userId");
			
			if(ToolValid.isNotEmpty(userId)){
				User user = userService.getUser(Integer.parseInt(userId));
				if(ToolValid.isNotNull(user)){
					defaultVehicleType = user.getDefaultVehicleType();
				}
			}
			
			map.put("defaultVehicleType", defaultVehicleType);
		}
		return ResultGenerator.genSuccessResult(map);
	}
	
//	/**
//	 * 获取指定距离内的停车场
//	 * @param lon		经度
//	 * @param lat		纬度
//	 * @param distance	距离
//	 * @return
//	 */
//	@RequestMapping("/getParkingInDistance")
//	public Result<List<ParkingSite>> getParkingInDistance(double lon, double lat, @RequestParam(value = "distance", required = false, defaultValue = "2000")int distance){
//		List<ParkingSite> pList = null;
//		List<Parking> list = parkingService.getAllParkingSiteList();
//		if(ToolValid.isNotEmpty(list)) {
//			pList = new ArrayList<>();
//			double d = 0;
//			ParkingSite ps = null;
//			for(Parking p: list) {
//				if(ToolValid.isNotBlank(p.getGpsX()) && ToolValid.isNotBlank(p.getGpsY())) {
//					d = GaodeDistance.getDistance(lon, lat, Double.parseDouble(p.getGpsX()), Double.parseDouble(p.getGpsY()));
//					if(distance > d) {
//						ps = new ParkingSite(p.getId(), p.getName(), p.getAddress(), p.getGpsX(), p.getGpsY(), d, true);
//						pList.add(ps);
//					}
//				}
//			}
//		}
//		
//		return ResultGenerator.genSuccessResult(pList);
//	}
	
	/**
	 * 获取指定距离内的停车场
	 * @param lon		经度
	 * @param lat		纬度
	 * @param distance	距离
	 * @return
	 */
	@RequestMapping("/getParkingInDistance")
	public Result<List<ParkingSite>> getParkingInDistance(double lon, double lat, @RequestParam(value = "distance", required = false, defaultValue = "2000")int distance){
		List<ParkingSite> pList = null;
		Map<String, Object> map = new HashMap<>();
		map.put("gpsX", lon);
		map.put("gpsY", lat);
		map.put("distance", distance);
		map.put("start", "0");
		map.put("limit", "5");
		List<SimpleParking> list = parkingService.getDistanceParking(map);
		map.put("lng", lon);
		map.put("lat", lat);
		List<SimpleEcdemicParking> list2 = ecdemicParkingService.getDistanceEcdemicParking(map);
		
		ParkingSite pSite = null;
		pList = new ArrayList<>();
		if(ToolValid.isNotEmpty(list)) {
			for(SimpleParking sp: list) {
				pSite = new ParkingSite(sp.getId(), sp.getName(), sp.getAddress(), sp.getGpsX(), sp.getGpsY(), sp.getDistance(), true);
				pList.add(pSite);
			}
		}
		
		if(ToolValid.isNotEmpty(list2)) {
			for(SimpleEcdemicParking sep: list2) {
				pSite = new ParkingSite(sep.getId(), sep.getName(), sep.getAddress(), sep.getLng(), sep.getLat(), sep.getDistance(), false);
				pList.add(pSite);
			}
		}
		
		Collections.sort(pList, new Comparator<ParkingSite>() {
            @SuppressWarnings("unused")
			@Override
            public int compare(ParkingSite o1, ParkingSite o2) {
                Collator collator = Collator.getInstance(Locale.CHINA);
                return Double.compare(o1.getDistance(), o2.getDistance());
            }
        });
		return ResultGenerator.genSuccessResult(pList);
	}
	
	/**
	 * 搜索指定关键字停车场
	 * 
	 * @param keyword
	 * @param lon
	 * @param lat
	 * @return
	 */
	@RequestMapping("/searchParking")
	public Result<List<ParkingSite>> searchParking(String keyword, double lon, double lat, int page_num){
		List<ParkingSite> pList = null;
		Map<String, Object> map = new HashMap<>();
		map.put("keyword", keyword);
		if(page_num<1) {
			map.put("start", 0);
		}
		else {
			map.put("start", 5 * (page_num + 1));
		}
		
		map.put("limit", "5");
		
		map.put("gpsX", lon);
		map.put("gpsY", lat);
		List<SimpleParking> list = parkingService.getDistanceParking(map);
		
		map.put("lng", lon);
		map.put("lat", lat);
		List<SimpleEcdemicParking> list2 = ecdemicParkingService.getDistanceEcdemicParking(map);
		
		ParkingSite pSite = null;
		pList = new ArrayList<>();
		if(ToolValid.isNotEmpty(list)) {
			for(SimpleParking sp: list) {
				pSite = new ParkingSite(sp.getId(), sp.getName(), sp.getAddress(), sp.getGpsX(), sp.getGpsY(), sp.getDistance(), true);
				pList.add(pSite);
			}
		}
		
		if(ToolValid.isNotEmpty(list2)) {
			for(SimpleEcdemicParking sep: list2) {
				pSite = new ParkingSite(sep.getId(), sep.getName(), sep.getAddress(), sep.getLng(), sep.getLat(), sep.getDistance(), false);
				pList.add(pSite);
			}
		}
		
		Collections.sort(pList, new Comparator<ParkingSite>() {
            @SuppressWarnings("unused")
			@Override
            public int compare(ParkingSite o1, ParkingSite o2) {
                Collator collator = Collator.getInstance(Locale.CHINA);
                return Double.compare(o1.getDistance(), o2.getDistance());
            }
        });
		return ResultGenerator.genSuccessResult(pList);
	}
	
}

@Value
class ParkingSite {
	private int id;
	private String name;
	private String address;
	private String lon;
	private String lat;
	private double distance;
	private boolean isLocal;
}
